﻿// Copyright (c)  YISH. All rights reserved.
// Licensed under the MIT License, See License.txt in the project root for license information.


using System;
using NSQL.Sharpen;

namespace NSQL.Expressions
{
    public class LogicalExpression : BaseExpression, ILogicalExpression
    {
        public IPredicateExpression Left { get; set; }
        public IPredicateExpression Right { get; set; }

        public LogicalOperator Operator { get; set; }

        public LogicalExpression()
        {
        }

        public LogicalExpression(IPredicateExpression left, IPredicateExpression right, LogicalOperator op)
        {
            Left = left;
            Right = right;
            Operator = op;
        }

        public override T Accept<T>(IExpressionVisitor<T> visitor) => visitor.Visit(this);
        public override void Accept(IExpressionVisitor visitor) => visitor.Visit(this);
        public override bool IsValid => !(Left is null || Right is null) && Left.IsValid && Right.IsValid;
        protected override IExpression DeepCopy()
        {
            return new LogicalExpression(Left.DeepClone(), Right.DeepClone(), Operator)
            {
                Tag = Tag
            };
        }

        public static LogicalOperator ParseOperator(string op)
        {
            if (Enum.TryParse(op, out LogicalOperator result))
            {
                return result;
            }

            throw new Exception("UnExpected group operator.expecting {And, Or}");
        }
    }
}